<template>
  <el-dialog title="用户表单" v-model="props.visible" width="30%" @close="close">
    <el-form :model="form" ref="formRef" label-width="120px">
      <el-form-item label="Name" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="ID" prop="id">
        <el-input v-model="form.id" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="close">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  form: {
    type: Object as () => {
      name: string;
      id: string;
    },
    default: () => ({
      name: "",
      id: "",
    }),
  },
});

const emit = defineEmits(["close", "submit"]); // 定义事件 (不需要校验参数)

const isEdit = ref(!!props.form?.id); // 是否编辑状态

/**
 * 关闭弹窗
 */
const close = () => {
  emit("close");
};

/**
 * 提交表单
 */
const submit = () => {
  // console.log("submit", props.form);
  // console.log("是否编辑", isEdit.value);
  emit("submit", props.form, isEdit.value);
};

console.log('UserForm组件实例',getCurrentInstance());

defineOptions({
  name: "UserForm", // 定义组件名称为 UserForm
});
</script>
